/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.modules.admin.entity.PartsPrice;
import org.springblade.modules.admin.entity.PartsPriceLeave;
import org.springblade.modules.admin.service.IPartsPriceService;
import org.springblade.modules.admin.vo.PartsPriceLeaveVO;
import org.springblade.modules.admin.mapper.PartsPriceLeaveMapper;
import org.springblade.modules.admin.service.IPartsPriceLeaveService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 配件价格等级表 服务实现类
 *
 * @author ysq
 * @since 2023-08-09
 */
@AllArgsConstructor
@Service
public class PartsPriceLeaveServiceImpl extends BaseServiceImpl<PartsPriceLeaveMapper, PartsPriceLeave> implements IPartsPriceLeaveService {

	private final IPartsPriceService partsPriceService;

	@Override
	public IPage<PartsPriceLeaveVO> selectPartsPriceLeavePage(IPage<PartsPriceLeaveVO> page, PartsPriceLeaveVO partsPriceLeave) {
		synchronized (this) {
			int total = this.count(Condition.getQueryWrapper(partsPriceLeave));
			if (total == 0) {
				PartsPriceLeave saveDefaultLeave = new PartsPriceLeave();
				saveDefaultLeave.setName("标准售价");
				this.save(saveDefaultLeave);
			}
		}
		return page.setRecords(baseMapper.selectPartsPriceLeavePage(page, partsPriceLeave));
	}

	@Override
	public boolean batchRemovePriceLeave(List<Long> ids) {
		int checkDel = partsPriceService.count(Wrappers.<PartsPrice>lambdaQuery().in(PartsPrice :: getPriceLeaveId, ids));
		if (checkDel > 0) {
			throw new ServiceException("价格等级已关联数据，无法删除");
		}
		return this.deleteLogic(ids);
	}
}
